package com.agricultural.machine.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 用户管理器类
 */

public class UserManager {
    private static UserManager instance;
    private final Map<String, User> users;
    private final UserSessionManager sessionManager;
    private UserSession currentSession;
    
    private UserManager() {
        this.users = new HashMap<>();
        this.sessionManager = UserSessionManager.getInstance();
        
        // 创建默认用户
        createDefaultUsers();
    }
    
    public static synchronized UserManager getInstance() {
        if (instance == null) {
            instance = new UserManager();
        }
        return instance;
    }
    
    /**
     * 创建用户
     */
    public boolean createUser(String username, String password, UserRole role) {
        if (users.containsKey(username)) {
            System.out.println("用户已存在: " + username);
            return false;
        }
        
        User user = new User(username, password, role);
        users.put(username, user);
        System.out.println("创建用户成功: " + username + ", 角色: " + role.getDescription());
        return true;
    }
    
    /**
     * 用户登录
     */
    public Optional<UserSession> login(String username, String password) {
        User user = users.get(username);
        if (user != null && user.getPassword().equals(password)) {
            // 创建新会话
            UserSession session = sessionManager.createSession(username, user.getRole());
            currentSession = session;
            System.out.println("用户 " + username + " 登录成功");
            return Optional.of(session);
        }
        System.out.println("用户名或密码错误");
        return Optional.empty();
    }
    
    /**
     * 用户注销
     */
    public boolean logout() {
        if (currentSession != null) {
            boolean result = sessionManager.terminateSession(currentSession.getSessionId());
            if (result) {
                System.out.println("用户 " + currentSession.getUsername() + " 已注销");
                currentSession = null;
                return true;
            }
        }
        return false;
    }
    
    /**
     * 更改密码
     */
    public boolean changePassword(String username, String oldPassword, String newPassword) {
        User user = users.get(username);
        if (user != null && user.getPassword().equals(oldPassword)) {
            user.setPassword(newPassword);
            System.out.println("用户 " + username + " 密码修改成功");
            return true;
        }
        return false;
    }
    
    /**
     * 修改用户角色
     */
    public boolean changeRole(String username, UserRole newRole) {
        // 只有管理员可以修改角色
        if (currentSession != null && currentSession.getRole() == UserRole.ADMIN) {
            User user = users.get(username);
            if (user != null) {
                user.setRole(newRole);
                System.out.println("用户 " + username + " 角色修改为: " + newRole.getDescription());
                return true;
            }
        } else {
            System.out.println("权限不足，无法修改用户角色");
        }
        return false;
    }
    
    /**
     * 获取所有用户
     */
    public List<User> getAllUsers() {
        return new ArrayList<>(users.values());
    }
    
    /**
     * 获取用户
     */
    public Optional<User> getUser(String username) {
        return Optional.ofNullable(users.get(username));
    }
    
    /**
     * 删除用户
     */
    public boolean deleteUser(String username) {
        // 只有管理员可以删除用户
        if (currentSession != null && currentSession.getRole() == UserRole.ADMIN) {
            if (users.remove(username) != null) {
                System.out.println("用户 " + username + " 已删除");
                return true;
            }
        } else {
            System.out.println("权限不足，无法删除用户");
        }
        return false;
    }
    

    public boolean hasPermission(UserRole requiredRole) {
        if (currentSession == null) {
            return false;
        }
        
        UserRole currentRole = currentSession.getRole();
        
        // 管理员拥有所有权限
        if (currentRole == UserRole.ADMIN) {
            return true;
        }
        
        // 操作员可以执行操作员和访客权限的操作
        if (currentRole == UserRole.OPERATOR) {
            return requiredRole == UserRole.OPERATOR || requiredRole == UserRole.VIEWER;
        }
        
        // 访客只能执行访客权限的操作
        return currentRole == requiredRole;
    }
    
    /**
     * 获取当前会话
     */
    public UserSession getCurrentSession() {
        return currentSession;
    }
    
    /**
     * 创建默认用户
     */
    private void createDefaultUsers() {
        // 创建管理员
        createUser("macheng", "123", UserRole.ADMIN);
        
        // 创建默认操作员
        createUser("operator", "op123", UserRole.OPERATOR);
        
        // 创建默认访客
        createUser("guest", "guest123", UserRole.VIEWER);
    }
} 